package com.fittinghome.Web;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.fittinghome.entity.FitOrder;
import com.fittinghome.entity.FitOrderdetail;
import com.fittinghome.service.IFitOrderService;
import com.fittinghome.service.IFitOrderdetailService;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @description:订单控制器
 * @package com.fittinghome.Web
 * @who MiaoMiao "Sprout" BI
 * @date 2021-04-14 13:21:46
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IFitOrderService orderService;

    @Autowired
    private IFitOrderdetailService orderdetailService;

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("created")
    public boolean created()
    {
//        FitOrder fitOrder = new FitOrder(){};
//        fitOrder.setGid(UUID.randomUUID().toString());
//        fitOrder.setOrderNo("SO"+ ThreadLocalRandom.current().nextInt());
//        fitOrder.setOrderType("1");
//        fitOrder.setOrderStatus("1");
//        fitOrder.setOrderAmount(Double.parseDouble("566.56"));
//        fitOrder.setOrderQty(Double.parseDouble("10"));
//        fitOrder.setReceiveName("sprout");

        FitOrderdetail orderDetail = new FitOrderdetail();
        orderDetail.setGid(UUID.randomUUID().toString());
        orderDetail.setOrderGid("A589A99E-9E67-4FFB-9615-7D7A42915240");
        orderDetail.setBarCode("A56A6S2232");
        orderDetail.setProdGid(UUID.randomUUID().toString());
        orderDetail.setSkuGid(UUID.randomUUID().toString());
        orderDetail.setSkuCode("5966533166");
        orderDetail.setProdName("测试商品");


        return orderdetailService.save(orderDetail);
    }

    /*单表 */
    @GetMapping("info")
    public FitOrder get()
    {
        return orderService.getById("A589A99E-9E67-4FFB-9615-7D7A42915240");
    }
    
    /**
     * @Description: 连表查询
     * @return: com.fittinghome.entity.FitOrder
     * @Author: MiaoMiao "Sprout" BI
     * @Date: 2021-52-14 14:52
     */
    public FitOrder detail()
    {
        return null;
    }

    @GetMapping("api")
    public boolean apiGet()
    {
        String url = "http://dev.sydj520.com:8086/udx/service";

        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.set("Udx-Method","UdxStd.StdLoginDomain.Login");
        httpHeaders.set("Udx-Service","CenterService");

        String body = "{\"ObjectData\":{\"UserCategory\":\"平台\",\"UserId\":\"dev\",\"Password\":\"dev\"},\"UniqueKey\":\"UdxStd.StdLoginDomain\",\"MethodName\":\"Login\",\"ClientTime\":\"2021-04-14 10:15:09\",\"NullValueIgnore\":false}";
        HttpEntity<JSONObject> httpEntity = new HttpEntity(body,httpHeaders);
        String response = restTemplate.postForObject(url, httpEntity, String.class);
        return true;
    }

    @RequestMapping("detail")
    public String orderDetail()
    {
        List<FitOrder> fitOrders = orderService.selectOrders("A589A99E-9E67-4FFB-9615-7D7A42915240");
        return JSON.toJSONString(fitOrders);
    }
}
